﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace XmHttpServer
{
    public class SerializeUtil
    {
        public static string Object2Xml(object obj)
        {
            //Type type = obj.GetType();
            //MemoryStream Stream = new MemoryStream();
            //XmlSerializer xml = new XmlSerializer(type);
            ////序列化对象  
            //xml.Serialize(Stream, obj);
            //Stream.Position = 0;
            //StreamReader sr = new StreamReader(Stream);
            //string str = sr.ReadToEnd();
            //sr.Dispose();
            //Stream.Dispose();
            //return str;
            return Tools.SerializeToXml(obj);
        }
        public static object Xml2Object(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }  
        }
        public static string Object2Json(object obj)
        {
            return JavaScriptConvert.SerializeObject(obj);
        }
        public static object Json2Object(Type type,string json)
        {
            return JavaScriptConvert.DeserializeObject(json, type);
        }




    }
}
